/**************************************************************************
** Yabet is bloging editor tool that supports many blog's formats. It is very usefull for crossposting, allows easily to
** make posts to your blogs.
**
** Copyright (C) 2010 Valery Konchin <eeexception@gmail.com>
**
** This program is free software: you can redistribute it and/or modify
**     it under the terms of the GNU General Public License as published by
**     the Free Software Foundation, either version 3 of the License, or
**     (at your option) any later version.
**
**     This program is distributed in the hope that it will be useful,
**     but WITHOUT ANY WARRANTY; without even the implied warranty of
**     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**     GNU General Public License for more details.
**
**     You should have received a copy of the GNU General Public License
**     along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

#include "markupmanager.h"
#include "markuploader.h"
#include <QRegExp>
#include <QDebug>
#include "defaulthtmlyabettextmarkup.h"

const YabetLogHandler* MarkupManager::log = new YabetLogHandler("MarkupManager");

MarkupManager::MarkupManager()
{
    MarkupLoader loader;

    YabetTextMarkup *defaultMarkup = new DefaultHtmlYabetTextMarkup();
    this->markups.insert(defaultMarkup->getMarkupName(), defaultMarkup);

    this->markups.unite(loader.getMarkupsList());

    if(log->isInfoEnabled() == true) {
        foreach(QString markupName, markups.keys()) {
            log->infoMsg("Loaded markup: " + markupName);
        }
    }
}

YabetTextMarkup* MarkupManager::getMarkup(QString markupName) {
    log->debugMsg("Markups contains [markup=" + markupName + "; " + markups.contains(markupName) + "]");

    YabetTextMarkup *markup = markups.value(markupName, new DefaultHtmlYabetTextMarkup());

    return markup;
}

QStringList MarkupManager::getMarkupNames() {
    return QStringList(markups.keys());
}

QString MarkupManager::convertText(QString text, YabetTextMarkup *fromMarkup, YabetTextMarkup *toMarkup) {
    QString convertedText = text;

    if(fromMarkup->getMarkupName() == toMarkup->getMarkupName()) {
        return convertedText;
    }

    foreach(QString markupElementName, fromMarkup->getMarkupElementsNames()) {
        if(markupElementName.isEmpty() == true) {
            continue;
        }

        if(fromMarkup->getMarkupElement(markupElementName)->getMarkupName().isEmpty() == true) {
            continue;
        }

        for(int i = 0; i < fromMarkup->getMarkupElement(markupElementName)->getMarkupParts().length(); i++) {
            if(toMarkup->getMarkupElement(markupElementName)->getMarkupName().isEmpty() == true) {
                log->debugMsg("To markup element is empty. Replacing with empty strings");

                //TODO: why??? have some problems with this.
//                convertedText = convertedText.replace(QRegExp::escape(fromMarkup->getMarkupElement(markupElementName)->getMarkupParts().at(i)), "");

                continue;
            }

            if(log->isDebugEnabled() == true) {
                log->debugMsg("Replace element [" + QRegExp::escape(fromMarkup->getMarkupElement(markupElementName)->getMarkupParts().at(i)) + "]");
                log->debugMsg("with element [" + toMarkup->getMarkupElement(markupElementName)->getMarkupParts().at(i) + "]");
            }

            convertedText = convertedText.replace(QRegExp::escape(fromMarkup->getMarkupElement(markupElementName)->getMarkupParts().at(i)),
                                                  toMarkup->getMarkupElement(markupElementName)->getMarkupParts().at(i));
        }
    }

    return convertedText;
}
